import { handleActions } from 'redux-actions'
import util from '../util'

const INIT_STATE = {
  projects: [],
  templates: {reviewTemplates: []},
  invitations: [],
  pendingSelfReviews: [],
  reviewItemInvitations: {invitedUsers:[], managerUser: {}},
  unreadyNum: 0,
  reviewItem: {},
  reviewItemProjects: [],
  reviewList: {},
  selfTemplates: {},
  peerTemplates: {},
};

export default handleActions({
  'fetch review navs': (state, { payload }) => {
    return {
      ...state,
      navs: payload
    }
  },
  'fetch review invitations': (state, { payload }) => {
    return {
      ...state,
      invitations: payload
    }
  },
  'fetch pending self reviews': (state, { payload }) => {
    return {
      ...state,
      pendingSelfReviews: payload
    }
  },
  'fetch pending peer reviews': (state, { payload }) => {
    return {
      ...state,
      pendingPeerReviews: payload
    }
  },
  'fetch canceled peer reviews': (state, { payload }) => {
    return {
      ...state,
      canceledPeerReviews: payload
    }
  },
  'fetch finished peer reviews': (state, { payload }) => {
    return {
      ...state,
      finishedPeerReviews: payload
    }
  },
  'fetch finished self reviews': (state, { payload }) => {
    return {
      ...state,
      finishedSelfReviews: payload
    }
  },
  'fetch review item': (state, { payload }) => {
    return {
      ...state,
      reviewItem: payload,
      reviewItemProjects: payload.projects
    }
  },
  'fetch review item projects': (state, { payload }) => {
    return {
      ...state,
      reviewItemProjects: payload.projects
    }
  },
  'clear review item': (state, action) => {
    return (INIT_STATE);
  },
  'fetch review item invitations': (state, { payload }) => {
    return {
      ...state,
      reviewItemInvitations: payload
    }
  },
  'fetch review project': (state, { payload }) => {
    const { index, ...others } = payload
    let newProjects = util.deepClone(state.reviewItemProjects || [])
    newProjects[index] = payload
    return {
      ...state,
      reviewItemProjects: newProjects
    }
  },
  'create review project': (state, { payload }) => {
    return {
      ...state,
      reviewItemProjects: [
        ...state.reviewItemProjects,
        {
          projectId: payload.idValue,
        },
      ]
    }
  },
  'delete review project': (state, { payload }) => {
    const {
      reviewItemProjects
    } = state;
    return {
      ...state,
      reviewItemProjects: [
        ...reviewItemProjects.filter(v => v.projectId !== payload)
      ]
    }
  },
  'update review project': (state, { payload }) => {
    const {
      reviewItemProjects
    } = state;
    return {
      ...state,
      reviewItemProjects: [
        ...reviewItemProjects.map(v => v.projectId !== payload.projectId ? v : payload)
      ]
    }
  },
  'fetch review templates': (state, { payload }) => {
    return {
      ...state,
      templates: payload
    }
  },
  'add reviewee comment': (state, { payload }) => {
    const newComment = {commentId: payload.data.idValue, content: payload.post.content}
    const newQuestions = state.reviewItem.questions.map((v,i) => {
          if (v.questionId == payload.questionId) {
            return {
              ...v,
              revieweeComment: {
                commentId: payload.data.idValue,
                content: payload.post.content
              }
            }
          }
          return v
        })
    return {
      ...state,
      reviewItem: {
        ...state.reviewItem,
        questions: newQuestions 
      }
    }
  },
  'update reviewee comment': (state, {payload}) => {
    const {
      activityId,
      questionId,
      commentId,
      data,
    } = payload;
    const {
      questions, 
    } = state.reviewItem;
    const newQuestions = state.reviewItem.questions.map((v,i) => {
      if (v.questionId === questionId && v.revieweeComment.commentId === commentId) {
        return {
          ...v,
          revieweeComment: {
            ...v.revieweeComment,
            content: data.content,
          }
        }
      }
      return v;
    })
    return {
      ...state,
      reviewItem: {
        ...state.reviewItem,
        questions: newQuestions 
      }
    }
  },
  'update reviewer comment': (state, {payload}) => {
    const {
      invitationId,
      questionId,
      commentId,
      data,
    } = payload;
    const {
      questions, 
    } = state.reviewItem;
    const newQuestions = state.reviewItem.questions.map((v,i) => {
      if (v.questionId === questionId && v.reviewerComment.commentId === commentId) {
        return {
          ...v,
          reviewerComment: {
            ...v.reviewerComment,
            content: data.content,
          }
        }
      }
      return v;
    })
    return {
      ...state,
      reviewItem: {
        ...state.reviewItem,
        questions: newQuestions 
      }
    }
  },
  'update reviewer project comment' : (state, {payload}) => {
    const {
      invitationId,
      projectId,
      commentId,
      data,
    } = payload;
    const newProjects = state.reviewItemProjects.map((v,i) => {
      if (v.projectId === projectId && v.reviewerComment.commentId === commentId) {
        return {
          ...v,
          reviewerComment: {
            ...v.reviewerComment,
            content: data.content,
          }
        }
      }
      return v;
    })
    return {
      ...state,
      reviewItemProjects: newProjects,
    }
  },
  'add reviewer comment': (state, { payload }) => {
    const newComment = {commentId: payload.data.idValue, content: payload.post.content}
    const newQuestions = state.reviewItem.questions.map((v,i) => {
          if (v.questionId == payload.questionId) {
            return {
              ...v,
              reviewerComment: {
                commentId: payload.data.idValue,
                content: payload.post.content
              }
            }
          }
          return v
        })
    return {
      ...state,
      reviewItem: {
        ...state.reviewItem,
        questions: newQuestions 
      }
    }
  },
  'add reviewer project comment': (state, { payload }) => {
    const newComment = {commentId: payload.data.idValue, content: payload.post.content}
    const newProjects = state.reviewItemProjects.map((v,i) => {
          if (v.projectId == payload.projectId) {
            return {
              ...v,
              reviewerComment: {
                commentId: payload.data.idValue,
                content: payload.post.content
              }
            }
          }
          return v
        })
    return {
      ...state,
      reviewItemProjects: newProjects,
    }
  },
  'fetch review report': (state, { payload }) => {
    return {
      ...state,
      reviewReport: payload
    }
  },
  'fetch review report activities': (state, { payload }) => {
    return {
      ...state,
      reviewReportActivities: payload
    }
  },
  'create review template': (state, { payload }) => {
    return {
      ...state,
      templates: {
        ...state.templates,
        reviewTemplates: [
          payload,
          ...state.templates.reviewTemplates
        ]
      }
    }
  },
  
  'increase unready by one': (state, { payload }) => {
    return {
      ...state,
      unreadyNum: state.unreadyNum + 1
    }
  },
  'decrease unready by one': (state, { payload }) => {
    return {
      ...state,
      unreadyNum: state.unreadyNum - 1
    }
  },





  'fetch review list': (state, {payload}) => {
    return {
      ...state,
      reviewList: payload,
    }
  },
  'fetch self review templates' : (state, {payload}) => {
    return {
      ...state,
      selfTemplates: payload,
    }
  },
  'fetch peer review templates' : (state, {payload}) => {
    return {
      ...state,
      peerTemplates: payload,
    }
  },



}, INIT_STATE)
